//
//  ThreeMealsViewController.m
//  味·道
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "ThreeMealsViewController.h"
#import "RecommendModel.h"
#import "Recommend2Model.h"
#import "MaocHeader.h"
#import "FirstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "ThirdTableViewCell.h"
#import "Recommend3Model.h"
#import "DetailViewController.h"
@interface ThreeMealsViewController ()
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *wucanArr;
@property(nonatomic,retain)NSMutableArray *wancanArr;
@property(nonatomic,retain)NSMutableArray *zaocanArr;
@property(nonatomic,retain)RecommendModel *firstModel;
@property(nonatomic,retain)Recommend3Model *wucanModel;
@property(nonatomic,retain)Recommend3Model *wancanModel;
@property(nonatomic,retain)Recommend3Model *zaocanModel;
@property(nonatomic,retain)RequestManager *request;

@end

@implementation ThreeMealsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatRandomButton];
    [self requestData];
    [self creatTableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shakeAction) name:@"shake" object:nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)requestData
{
    self.request = [[RequestManager alloc] initWithRequestURL:@"http://ibaby.ipadown.com/api/food/food.plan.detail.php?random=1" requestModel:GET requestParameter:nil];
    self.request.delegate  = self ;
    [self.request startRequest];
}
//-(BOOL)canBecomeFirstResponder
//{
//    return YES;
//}
//-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    [self requestData];
//    [self creatTableView];
//}
//-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//
//}
-(void)shakeAction
{
    [self requestData];
    [self creatTableView];
}
-(void)creatTitle
{
    
}
-(void)creatRandomButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300, 30, 70, 20);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(random:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
-(void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate  = self;
    [self.tableView registerClass: [FirstTableViewCell class]forCellReuseIdentifier:@"first"];
    [self.tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"second"];
    [self.tableView registerClass:[ThirdTableViewCell class] forCellReuseIdentifier:@"third"];
    [self.view addSubview:self.tableView];
    
    
}

#pragma mark - requestDelegate
-(void)requestDidFinishWithData:(NSData *)data
{
    //对中午早进行初始化
    self.wancanArr  = [[NSMutableArray alloc] initWithCapacity:0];
    self.wucanArr = [[NSMutableArray alloc] initWithCapacity:0];
    self.zaocanArr = [[NSMutableArray alloc] initWithCapacity:0];
    self.zaocanModel = [[Recommend3Model alloc] init];
    self.firstModel = [[RecommendModel alloc] init];
    self.wucanModel = [[Recommend3Model alloc] init];
    self.wancanModel = [[Recommend3Model alloc] init];
    
    //最上面的cell数据
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
    [_firstModel setValuesForKeysWithDictionary:dict];
    
    //晚餐数据
    NSDictionary *dict2 = [dict objectForKey:@"wancan"];
    [self.wancanModel setValuesForKeysWithDictionary:dict2];
    NSArray *wancan = [dict2 objectForKey:@"chi"];
    for (NSDictionary *dic in wancan) {
        Recommend2Model *model = [[Recommend2Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.wancanArr addObject:model];
    }
    
    //午餐数据
    NSDictionary *wucandict2 = [dict objectForKey:@"wucan"];
    [self.wucanModel setValuesForKeysWithDictionary:wucandict2];
    NSArray *wucan = [wucandict2 objectForKey:@"chi"];
    for (NSDictionary *dic in wucan) {
        Recommend2Model *model = [[Recommend2Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.wucanArr addObject:model];
    }
    
    //早餐数据
    NSDictionary *zaocandict = [dict objectForKey:@"zaocan"];
    [self.zaocanModel setValuesForKeysWithDictionary:zaocandict];
    NSArray *zaocan = [zaocandict objectForKey:@"chi"];
    for (NSDictionary *dic in zaocan) {
        Recommend2Model *model = [[Recommend2Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.zaocanArr addObject:model];
    }
    
    [self.tableView reloadData];
    
    
}
-(void)requestError:(NSError *)error
{
    NSLog(@"%@",error);
}



#pragma mark - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 2;
            break;
        default:
            return 0;
            break;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"first" forIndexPath:indexPath];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            [cell setModel:self.firstModel];
            return cell;
            break;
        }
        case 1:
        {
            SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"second" forIndexPath:indexPath];
            [cell setModel:self.zaocanModel];
            return cell;
            break;
        }
        case 2:
        {
            ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"third" forIndexPath:indexPath];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            if (indexPath.row == 0) {
                cell.arr = [NSMutableArray arrayWithArray:self.wucanArr];
                [cell setModel:self.wucanModel];
                cell.typeLable.text = @"午餐";
                return cell;
            }
            else{
            cell.arr = [NSMutableArray arrayWithArray:self.wancanArr];
            [cell setModel:self.wancanModel];
            cell.typeLable.text = @"晚餐";
            return cell;
            }
            break;
            
        }
        default:
            return nil;
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section) {
        case 0:
             return [FirstTableViewCell heightForCell:self.firstModel];
            break;
        case 1:
            return 200;
            break;
        case 2:
            return 450;
            break;
        default:
            return 0;
            break;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    Recommend2Model *model =  self.zaocanArr[indexPath.row];
    detailVC.ID = model.ID;
    [self.rightVC.navigationController pushViewController:detailVC animated:YES];
}

-(void)random:(UIButton *)button
{
    self.request = [[RequestManager alloc] initWithRequestURL:@"http://ibaby.ipadown.com/api/food/food.plan.detail.php?random=1" requestModel:GET requestParameter:nil];
    [self requestData];
    [self creatTableView];
}
/*;
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
